################################################
##  Main .R script                            ##
##  Replicates Tables and Graphs in           ##
## "Work Meaning and the Flexibility Puzzle"  ##
##  by Thimo De Schouwer and Iris Kesternich  ##
################################################

# Clear Environment
rm(list = ls())

## 0. SETUP
# Install the Environment
dir <- "C:/Users/desch/Dropbox/MeaningFlexibilityPuzzle/" # SET YOUR DIRECTORY

graph_dir <- file.path(dir, "GraphsTables/")
setwd(dir)


# Install / Load libraries
listOfPackages <- c("data.table","tidyverse","haven","rlang","dplyr",
                    "fastDummies","ggplot2","xtable","Hmisc", "psych",
                    "knitr","broom", "forcats", "RStata", "foreign")
for (i in listOfPackages){
  if(! i %in% installed.packages()){
    install.packages(i, dependencies = TRUE)
  }
  require(i)
}

library("data.table")
library("tidyverse")
library("haven")
library("rlang")
library("dplyr")
library("fastDummies")
library("ggplot2")
library("xtable")
library("Hmisc")
library("psych")
library("knitr")
library("broom")
library("foreign")
library("forcats")
library("RStata")


#################
# 0. IMPORT DATASETS
ISSP_data <- read_dta(file.path(dir, "DataCode/Data/Raw/ISSP/ISSP_2015.dta"))
LISS_data <- read_dta(file.path(dir, "DataCode/Data/Raw/LISS/L_HCE_5p.dta"))
#################


#################
# 1. CLEANING THE DATASETS
source(file.path(dir, "DataCode/Code/ISSP/1_Cleaning_ISSP.R"))
source(file.path(dir, "DataCode/Code/LISS/1_Cleaning_LISS.R"))

# Export the LISS data to estimate discrete choice models (in Stata)
write.csv(clean_LISS_data, file.path(dir, "DataCode/Data/Constructed/LISS/Clean_LISS.txt"), na="")
write.csv(clean_ISSP_data, file.path(dir, "DataCode/Data/Constructed/ISSP/Clean_ISSP.txt"), na="")
#################


#################
# 2. AMENTITIES: LEVELS AND PREFERENCES
## ISSP: LEVELS
source(file.path(dir, "DataCode/Code/ISSP/2_PlotAmenities_ISSP.R")) ## AMENITY PLOT FOR ISSP
print(plot_amenities_ISSP)

## ISSP: PREFERENCES
source(file.path(dir, "DataCode/Code/ISSP/2_PlotPrefAmenities_ISSP.R")) ## AMENITY PREF PLOT FOR ISSP
print(plot_pref_amenities_ISSP)
  
## LISS: LEVELS
source(file.path(dir, "DataCode/Code/LISS/2_PlotAmenities_LISS.R")) ## AMENITY PLOT FOR LISS
print(plot_amenities_LISS)
  
## LISS: PREFERENCES (CHOICE MODELS)
# STATA
#################


#################
# 3. MEANING-FLEXIBILITY RELATION
## 3.1. Cross-Tabulation - Meaning and Flexibility
source(file.path(dir, "DataCode/Code/ISSP/3_CrossTabulation_ISSP.R"))
source(file.path(dir, "DataCode/Code/LISS/3_CrossTabulation_LISS.R"))

## 3.2. Bell and OLS Estimators
source(file.path(dir, "DataCode/Code/ISSP/3_Proxy_ISSP.R"))
source(file.path(dir, "DataCode/Code/LISS/3_Proxy_LISS.R"))

## 3.3 LISS - Correlations Mechanism
source(file.path(dir, "DataCode/Code/LISS/3_Correlations_LISS.R"))

#################
# 4. COUNTERFACTUALS
# STATA
#################
  

#################
# A. APPENDIX RESULTS
# SAMPLE BACKGROUND TABLE
# STATA

# BELL FIRST STAGE
source(file.path(dir, "DataCode/Code/ISSP/Appendix/AppendixC_BellFS_ISSP.R"))
source(file.path(dir, "DataCode/Code/LISS/Appendix/AppendixC_BellFS_LISS.R"))
  
# GENDER EMPLOYMENT BY SECTOR
source(file.path(dir, "DataCode/Code/LISS/Appendix/AppendixD_EmploymentSector.R"))

#################
# O.A. ONLINE APPENDIX RESULTS

# TABLE PANDEMIC DIFFERENCE
source(file.path(dir, "DataCode/Code/LISS/Appendix/Online/AppendixC_CovidTable.R"))

# DISCRETE CHOICE MODELS APPENDIX
# STATA

# FULLTIME WORKERS
source(file.path(dir, "DataCode/Code/LISS/Appendix/Online/AppendixE_FullTime.R")) ## AMENITY PLOT FOR LISS
# STATA FOR PREF